<?php
namespace hyang;
use hyang\View;
class Controller
{
    public $selfViewParam;// 开启自定义模式string/array-关闭视图引擎
    public $ViewEngineName;// 视图引擎名称
    private $_ViewOn;// 视图被开启
    // 使用straits
    use \hyang\traits\View;
    public function __construct()
    {
        // 预处理
        $this->_ViewOn = (defined('View_Engine') && strtoupper(View_Engine) != 'OFF')? true:false;
        // 项目初始化
        $this->_init();
        // 视图
        if(!empty($this->selfViewParam)) $this->_ViewOn = true;
        if($this->_ViewOn) $this->viewUnit();
    }
    public function viewUnit($feek=null)
    {
        $sView = $this->_selfView();
        if(is_array($sView)) $opt = $sView;
        else{
            $opt = $this->selfViewParam? $this->selfViewParam:null;
            if(is_string($opt)) $opt = ['name'=>$opt];
        }
        $v = new View($opt);
        if($feek) return $v;
        $this->view = $v->getObject();
        $type = $v->getVEName();
        if($type) $this->ViewEngineName = $type;
    }
    /*ajax数据返回*/
    public function ajaxReturn($data,$type='json')
    {
        if(!is_array($data)) exit($data);//下面的方法js无法获取数据
		switch (strtoupper($type)){
            case 'JSON' :
                // 返回JSON数据格式到客户端 包含状态信息
                header('Content-Type:application/json; charset=utf-8');
                exit(json_encode($data,$json_option));
            case 'XML'  :
                // 返回xml格式数据
                header('Content-Type:text/xml; charset=utf-8');
                exit(xml_encode($data));
		}
    }
    /*base前后数据加密解密  post */
    public function getPost($filter=null)
    {
        $data = $_POST;
        if(isset($data['uid'])) unset($data['uid']);
        if(isset($data['ajax'])) unset($data['ajax']);
        if(isset($data['class_action'])) unset($data['class_action']);
        if(isset($data['bindDataOnUrl'])) unset($data['bindDataOnUrl']);        
        if(isset($data['encry'])){
            unset($data['encry']);
            foreach($data as $k=>$v){
                if(!is_string($v)) continue;
                $tmp = explode('//',base64_decode($v));
                $data[$k] = $tmp[0];
            }
        }
        //  数组过滤
        if(is_array($filter)){$data = array_diff_key($data,array_flip($filter));}
        elseif(is_string($filter) && !empty($filter)){
            if(array_key_exists($filter,$data)) return $data[$filter];//获取单一子数据
            else return '';
        } 
        return $data;
    }
    //  get 
    public function getQuery($filter=null)
    {
        $data = $_GET;
        if(isset($data['uid'])) unset($data['uid']);
        if(isset($data['ajax'])) unset($data['ajax']);
        if(isset($data['encry'])){
            unset($data['encry']);
            foreach($data as $k=>$v){
                if(!is_string($v)) continue;
                $tmp = explode('//',base64_decode($v));
                $data[$k] = $tmp[0];
            }
        }
        //  数组过滤
        if(is_array($filter)){$data = array_diff_key($data,array_flip($filter));}
        elseif(is_string($filter) && !empty($filter)){
            if(array_key_exists($filter,$data)) return $data[$filter];//获取单一子数据
            else return '';
        } 
        return $data;
    }
    /*处理接口*/
    public function _selfView($param=null){return 'unset';}// 自定义接口- 对应属性 $selfViewOn
    public function _init(){}
}